#include<cstdio>

int mm[10] = {1, 5, 10, 10, 10, 12};
int dd[10] = {1, 1, 1, 2, 3, 25};
int day[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

void nextday(int &y,int &m,int &d){
	d++;
	if(d == day[m]+1){
		d = 1;
		m++;
	}
}

int main(){
	int y,w,m,d,sf,ans;
	
	scanf("%d",&y);
	for(int i=6;i<10;i++){
		scanf("%d%d",&mm[i],&dd[i]);
	}
	scanf("%d",&w);
	
	if(y%100!=0&&y%4==0||y%400==0){
		day[2]++;
	}
	
	m = 1;
	d = 1;
	sf = 0;
	ans = 0;
	while(m<13){
		if(m == mm[6] && d == dd[6]){
			ans++;
			sf = 2;
		}else if(sf){
			ans++;
			sf--;
		}else if(w == 6 || w == 7){
			ans++;
		}else{
			for(int i=0;i<10;i++){
				if(m == mm[i] && d == dd[i]){
					ans++;
					break;
				}
			}
		}
		nextday(y,m,d);
		w++;
		if(w == 8){
			w = 1;
		}
	}
	printf("%d",ans);
	
	return 0;
} 
